package com.example.demo.module.repository.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.entity.enums.PurchaseRepositoryStateEnum;
import com.example.demo.common.entity.enums.RepositoryTypeEnum;
import com.example.demo.common.service.impl.BaseServiceImpl;
import com.example.demo.entity.pojo.repository.MaterialPoJo;
import com.example.demo.entity.pojo.repository.PurchaseRepositoryPoJo;
import com.example.demo.entity.pojo.repository.RepositoryPoJo;
import com.example.demo.entity.request.repository.PurchaseRepositoryQuery;
import com.example.demo.entity.request.repository.PurchaseRepositoryRequest;
import com.example.demo.module.repository.mapper.PurchaseRepositoryMapper;
import com.example.demo.module.repository.service.MaterialService;
import com.example.demo.module.repository.service.PurchaseRepositoryService;
import com.example.demo.module.repository.service.RepositoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;


/**
 * .
 *
 * @author Mei Ruoxiao
 * @date 2020/9/19
 */
@Service
@Slf4j
public class PurchaseRepositoryServiceImpl extends BaseServiceImpl<PurchaseRepositoryMapper, PurchaseRepositoryPoJo> implements PurchaseRepositoryService {

    @Resource
    private MaterialService materialService;
    @Resource
    private RepositoryService repositoryService;

    @Override
    public Page<PurchaseRepositoryPoJo> page(PurchaseRepositoryQuery query, long companyId) {
        log.info("page:[query:{}, companyId:{}]", query, companyId);
        Boolean state = query.getState();
        String brand = query.getBrand();
        Date start = query.getStart();
        Date end = query.getEnd();
        int page = query.getPage();
        int size = query.getSize();
        return page(Wrappers.<PurchaseRepositoryPoJo>lambdaQuery()
                        .eq(Objects.nonNull(state), PurchaseRepositoryPoJo::getState, state)
                        .eq(Objects.nonNull(brand), PurchaseRepositoryPoJo::getBrand, brand)
                        .ge(Objects.nonNull(start), PurchaseRepositoryPoJo::getCreateTime, start)
                        .le(Objects.nonNull(end), PurchaseRepositoryPoJo::getCreateTime, end)
                        .eq(PurchaseRepositoryPoJo::getCompanyId, companyId),
                page,
                size,
                OrderItem.desc("create_time")
        );
    }

    @Override
    public boolean warehouse(PurchaseRepositoryRequest request, long companyId) {
        log.info("warehouse:[request:{}, companyId:{}]", request, companyId);
        MaterialPoJo materialPoJo = new MaterialPoJo();
        BeanUtils.copyProperties(request, materialPoJo);
        //获取该企业的默认材料仓库文件夹
        RepositoryPoJo repositoryPoJo = repositoryService.getRootFolder(RepositoryTypeEnum.MATERIAL.value(), companyId);
        materialPoJo.setRepositoryId(repositoryPoJo.getId());
        materialPoJo.setType(RepositoryTypeEnum.PRODUCT.value());
        PurchaseRepositoryPoJo poJo = getById(request.getId());
        //以名称品牌规格为唯一，作为材料验证
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(request.getBrand()).append(request.getName()).append(request.getSpec());
        //判断材料仓库是否存在该材料
        MaterialPoJo find = materialService.get(stringBuilder.toString());
        if (Objects.nonNull(find)) {
            find.setStock(find.getStock() + request.getStock());
            //设置已入库数量
            poJo.setStock(poJo.getStock() + request.getStock());
        } else {
            materialPoJo.setVerify(stringBuilder.toString());
            materialService.create(materialPoJo);
            //设置已入库数量
            poJo.setStock(request.getStock());
        }
        //设置采购单入库状态
        if (Objects.equals(poJo.getStock(), request.getQuantity())) {
            poJo.setState(PurchaseRepositoryStateEnum.FINISH.value());
        }
        return updateById(poJo);
    }
}